package test.testtest;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.service.ApplicationContext;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.StreamResource.StreamSource;
import com.vaadin.terminal.StreamVariable;
import com.vaadin.terminal.gwt.server.AbstractWebApplicationContext;
import com.vaadin.terminal.gwt.server.WebBrowser;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.DragAndDropWrapper;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Html5File;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

public class DragDropHtml5ToFromDesktopExample extends VerticalLayout {
	private ProgressIndicator progress;

    public DragDropHtml5ToFromDesktopExample() {
        addComponent(new Label(
                "Drag text from a desktop application or an image file "
                        + "from the file system to the drop box below.<br/> "
                        + "Dragging files requires an HTML5 capable browser "
                        + "like Firefox, Safari or Chrome.<br/> "
                        + "You can also drag the drop box to a desktop "
                        + "application, getting either HTML or plain text "
                        + "depending on the application.", Label.CONTENT_XHTML));

        CssLayout dropPane = new CssLayout();
        dropPane.setWidth("200px");
        dropPane.setHeight("200px");
        dropPane.addStyleName("image-drop-pane");

        ImageDropBox dropBox = new ImageDropBox(dropPane);
        dropBox.setSizeUndefined();

        Panel panel = new Panel(dropBox);
        panel.setSizeUndefined();
        panel.addStyleName("no-vertical-drag-hints");
        panel.addStyleName("no-horizontal-drag-hints");
        addComponent(panel);

        progress = new ProgressIndicator();
        progress.setIndeterminate(true);
        progress.setVisible(false);
        addComponent(progress);
    }
    private class ImageDropBox extends DragAndDropWrapper implements
    DropHandler {
public ImageDropBox(Component root) {
			super(root);
			setDropHandler(this);
            setDragStartMode(DragStartMode.HTML5);
            setHTML5DataFlavor("text/plain",
                    "This text was drag'n'dropped from Vaadin.");
            setHTML5DataFlavor("text/html",
                    "<h1>HTML Content</h1><p>This text was drag&quot;n&quot;dropped from Vaadin.");
		}

		private static final long serialVersionUID = 1L;
private static final long FILE_SIZE_LIMIT = 2 * 1024 * 1024;

public void drop(DragAndDropEvent dropEvent) {
	  WrapperTransferable tr = (WrapperTransferable) dropEvent
              .getTransferable();
      Html5File[] files = tr.getFiles();
      if (files != null) {
          for (final Html5File html5File : files) {
              final String fileName = html5File.getFileName();

              if (html5File.getFileSize() > FILE_SIZE_LIMIT) {
                  getWindow()
                          .showNotification(
                                  "File rejected. Max 2MB files are accepted by Sampler",
                                  Notification.TYPE_WARNING_MESSAGE);
              } else {

                  final ByteArrayOutputStream bas = new ByteArrayOutputStream();
                  StreamVariable streamVariable = new StreamVariable() {

                      public OutputStream getOutputStream() {
                          return bas;
                      }

                      public boolean listenProgress() {
                          return false;
                      }

                      public void onProgress(StreamingProgressEvent event) {
                      }

                      public void streamingStarted(
                              StreamingStartEvent event) {
                      }

                      public void streamingFinished(
                              StreamingEndEvent event) {
                          progress.setVisible(false);
                         // showFile(fileName, html5File.getType(), bas);
                      }

                      public void streamingFailed(
                              StreamingErrorEvent event) {
                          progress.setVisible(false);
                      }

                      public boolean isInterrupted() {
                          return false;
                      }
                  };
                  html5File.setStreamVariable(streamVariable);
                  progress.setVisible(true);
              }
          }

      } else {
          String text = tr.getText();
          if (text != null) {
              showText(text);
          }
      }
  }

	
    private void showText(String text) {
        showComponent(new Label(text), "Wrapped text content");
    }
  /*  private void showFile(String name, String type,
            final ByteArrayOutputStream bas) {
        StreamSource streamSource = new StreamSource() {
            public InputStream getStream() {
                if (bas != null) {
                    byte[] byteArray = bas.toByteArray();
                    return new ByteArrayInputStream(byteArray);
                }
                return null;
            }
        };
        StreamResource resource = new StreamResource(streamSource, name,
                getApplication());

        Embedded embedded = new Embedded(name, resource);
        showComponent(embedded, name);
    }*/
    private void showComponent(Component c, String name) {
        VerticalLayout layout = new VerticalLayout();
        layout.setSizeUndefined();
        layout.setMargin(true);
        Window w = new Window(name, layout);
        w.setSizeUndefined();
        c.setSizeUndefined();
        w.addComponent(c);
        w.center();
        getWindow().addWindow(w);

    }
public AcceptCriterion getAcceptCriterion() {
	return null;
} // 2MB
    }
}
